package com.liushao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liushao.entity.BorrowInfo;
import com.liushao.entity.Borrower;
import com.liushao.entity.IntegralGrade;
import com.liushao.entity.UserInfo;
import com.liushao.entity.vo.BorrowInfoApprovalVO;
import com.liushao.entity.vo.BorrowerDetailVO;
import com.liushao.enums.BorrowInfoStatusEnum;
import com.liushao.enums.BorrowStatusEnum;
import com.liushao.mapper.BorrowInfoMapper;
import com.liushao.mapper.BorrowerMapper;
import com.liushao.mapper.IntegralGradeMapper;
import com.liushao.mapper.UserInfoMapper;
import com.liushao.service.BorrowInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liushao.service.BorrowerService;
import com.liushao.service.DictService;
import com.liushao.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {


    @Autowired
    private UserInfoMapper userInfoMapper;

    //积分等级mapper
    @Autowired
    private IntegralGradeMapper integralGradeMapper;

    @Autowired
    private DictService dictService;

    @Autowired
    private BorrowerMapper  borrowerMapper;

    @Autowired
    private BorrowerService borrowerService;

    @Autowired
    private LendService lendService;


    @Override
    public BigDecimal getBorrowAmount(Long userId) {

        //获取用户的积分
        UserInfo userInfo = userInfoMapper.selectById(userId);
        //判断用户不为空也是必要的
        Integer integral = userInfo.getIntegral();

        //根据积分获取借款额度
        QueryWrapper<IntegralGrade> wrapper = new QueryWrapper<>();
        wrapper.le("integral_start",integral);
        wrapper.ge("integral_end",integral);
         //拿到积分等级对象
        IntegralGrade integralGrade = integralGradeMapper.selectOne(wrapper);
        //判断为空
        if(integralGrade==null){
              return  new BigDecimal("0");
        }


        return integralGrade.getBorrowAmount();
    }

    @Override
    public Integer getBorrowInfoStatus(Long userId) {
        QueryWrapper<BorrowInfo> wrapper = new QueryWrapper<>();
        wrapper.select("status").eq("user_id",userId);

        List<Object> objects = baseMapper.selectObjs(wrapper);
        if(objects.size()==0){
               return BorrowStatusEnum.NO_BORROW.getStatus();
        }

       Integer status= (Integer) objects.get(0);

        return status;
    }

    @Override
    public void saveBorrowInfo(BorrowInfo borrowInfo, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);

        //判断用户绑定状态是否绑定了 必须绑定才行

        // 判断用户信息的认证状态 必须是审批通过才行

        //判断借款额度问题 ??    能借 10w 借15W
        //根据userId  获取借款额度
        BigDecimal borrowAmount = this.getBorrowAmount(userId);
        //判断一下
        if(borrowInfo.getAmount().doubleValue()>borrowAmount.doubleValue()){
          //不  可以借钱
        }

        //可以借钱
        borrowInfo.setUserId(userId);//借钱人
        borrowInfo.setBorrowYearRate(borrowInfo.getBorrowYearRate().divide(new BigDecimal(100)));
        borrowInfo.setStatus(BorrowInfoStatusEnum.BORROW_ING.getStatus());

        baseMapper.insert(borrowInfo);

    }

    @Override
    public List<BorrowInfo> selectList() {

        List<BorrowInfo> borrowInfoList = baseMapper.selectBorrowerInfoList();

        borrowInfoList.forEach(borrowInfo -> {
            String returnMethod =
                    dictService.getNameByParentDictCodeAndValue("returnMethod",
                            borrowInfo.getReturnMethod());
            String moneyUse =
                    dictService.getNameByParentDictCodeAndValue("moneyUse",
                            borrowInfo.getMoneyUse());

            String msgStatus = BorrowInfoStatusEnum.getMsgStatus(borrowInfo.getStatus());

            borrowInfo.getParam().put("returnMethod",returnMethod);
            borrowInfo.getParam().put("moneyUse",moneyUse);
            borrowInfo.getParam().put("status",msgStatus);


        });

        return borrowInfoList;
    }

    @Override
    public Map<String, Object> getBorrowDetailById(Long id) {
        //通过借款id 获取借款对象
        BorrowInfo borrowInfo = baseMapper.selectById(id);

        //数据组装
        String returnMethod =
                dictService.getNameByParentDictCodeAndValue("returnMethod",
                        borrowInfo.getReturnMethod());
        String moneyUse =
                dictService.getNameByParentDictCodeAndValue("moneyUse",
                        borrowInfo.getMoneyUse());

        String msgStatus = BorrowInfoStatusEnum.getMsgStatus(borrowInfo.getStatus());

        borrowInfo.getParam().put("returnMethod",returnMethod);
        borrowInfo.getParam().put("moneyUse",moneyUse);
        borrowInfo.getParam().put("status",msgStatus);


       // borrowInfo.getUserId()

        //条件构造器
        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",borrowInfo.getUserId());
        Borrower borrower = borrowerMapper.selectOne(wrapper);

        //数据组装
        BorrowerDetailVO borrowerDetailVO =
                borrowerService.getBorrowerDetailVOById(borrower.getId());

        //使用map
        Map<String,Object> map = new HashMap<>();

        map.put("borrowInfo",borrowInfo);
        map.put("borrower",borrowerDetailVO);

        return map;
    }

    @Override
    public void approval(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        //修改借款信息状态
        Long borrowInfoId = borrowInfoApprovalVO.getId();

        BorrowInfo borrowInfo = baseMapper.selectById(borrowInfoId);
        borrowInfo.setStatus(borrowInfoApprovalVO.getStatus());
        baseMapper.updateById(borrowInfo);

        //审核通过以后 创建标的 平台展示  让投资者进行选择投资
        if(borrowInfoApprovalVO.getStatus().intValue()
                ==BorrowInfoStatusEnum.BORROW_OK.getStatus().intValue()){
           //通过的情况下 创建 标的
           //TODO 做一些事情    创建 标的
            lendService.createLend(borrowInfoApprovalVO,borrowInfo);


        }

    }
}
